<?php
namespace haibao\jiepai\business;

abstract class Base{

    const ACCESS_DENIED = 1000;

    /**
     * @var array 海报cms后台用户与街拍cms后台用户对应关系
     */
    public static $userMap = array(
        4 => array('id' => 5, 'name' => '超管', 'nickname' => '超管'),
        643 => array('id' => 7, 'name' => '陈佳', 'nickname' => '编辑'),
        474 => array('id' => 10, 'name' => '罗一萱', 'nickname' => '街拍编辑'),
        581 => array('id' => 11, 'name' => '程杰', 'nickname' => '程杰'),
        351 => array('id' => 12, 'name' => '洪立人', 'nickname' => '洪立人'),
        602 => array('id' => 13, 'name' => '张涛', 'nickname' => '张涛'),
        573 => array('id' => 14, 'name' => '李燕', 'nickname' => '李燕'),
        654 => array('id' => 15, 'name' => '顾京怡', 'nickname' => '顾京怡'),
        582 => array('id' => 16, 'name' => '许稳', 'nickname' => 'ada'),
        670 => array('id' => 17, 'name' => '陆奕越', 'nickname' => 'jermie'),
        673 => array('id' => 18, 'name' => '曹瞰文', 'nickname' => 'Vear'),
        679 => array('id' => 20, 'name' => '孙弋岚', 'nickname' => 'sunyilan'),
        682 => array('id' => 22, 'name' => '黄海潮', 'nickname' => '黄海潮'),
        684 => array('id' => 23, 'name' => '陈华蓉', 'nickname' => '陈华蓉'),
        578 => array('id' => 24, 'name' => '郭怡悦', 'nickname' => '郭怡悦'),
        421 => array('id' => 25, 'name' => '梁健', 'nickname' => '梁健'),
        572 => array('id' => 26, 'name' => '靳悦', 'nickname' => 'jinyue'),
    );

    public static $defaultType = array(
            '1'=>"名人名流",
            '2'=>"影视明星",
            '3'=>"模特",
            '4'=>"音乐人",
            '5'=>"体育明星",
            '6'=>"时尚博主",
            '7'=>"时尚编辑",
            '8'=>"It Girl",
            '9'=>"王室成员",
            '10'=>"明星宝贝",
            '11'=>"风格偶像",
            '12'=>"美剧明星",
            '14'=>"主播",
            '15'=>"设计师",
            '16'=>"造型师",
            '17'=>"摄影师",
            '18'=>"导演",
            '19'=>"艺术家",
            '13'=>"其他"
        );
    public static $defaultPlace = array(
        '1'=>'欧美',
        '2'=>'日本',
        '3'=>'韩国',
        '4'=>'港台/东南亚',
        '5'=>'内地',
        '6'=>'其他',
    );

    private static $currentUser = null;

    public function __construct(){}

    public static function setCurrentUser($userModel){
        self::$currentUser = $userModel;
    }
    
    public static function getCurrentUser(){
        return self::$currentUser;
    } 
    public static function allowOperation($funcCode,$userPermission=\haibao\cms\model\data\User::PERMISSION_EMPLOYEE,$userId=null){
        $userModel = null;
        if ($userId){
            $userModel = \haibao\cms\business\User::getUserById($userId);
            if (!$userModel){
                return false;
            }
        }
        else{
            $userModel = self::getCurrentUser();
        }
        
        $allow = false;
        if($userModel){
            if ($userModel->Permission == \haibao\cms\model\data\User::PERMISSION_ADMIN){
                return true;
            }
            	
            switch ($userPermission){
                case \haibao\cms\model\data\User::PERMISSION_EMPLOYEE:
                    break;
                case \haibao\cms\model\data\User::PERMISSION_MANAGER:
                    if ($userModel->Permission == \haibao\cms\model\data\User::PERMISSION_EMPLOYEE){
                        return false;
                    }
                    break;
                case \haibao\cms\model\data\User::PERMISSION_ADMIN:
                    if ($userModel->Permission != \haibao\cms\model\data\User::PERMISSION_ADMIN){
                        return false;
                    }
                    break;
            }
            	
            foreach($userModel->Groups as $groupId){
                $funcCodes = UserGroup::getFuncCodesByGroupId($groupId);
                if (in_array($funcCode, $funcCodes)){
                    $allow = true;
                    break;
                }
            }
        }
    
        return $allow;
    }
    public static function checkOperation($funcCode,$userPermission=\haibao\cms\model\data\User::PERMISSION_EMPLOYEE,$userId=null){
        if (!self::allowOperation($funcCode,$userPermission,$userId)){
            throw new BusinessException('对不起，你无此权限，请联系系统管理员！',self::ACCESS_DENIED);
        }
    }
    
    public static function saveOperateHistory($operateType,$operateData,$userId = null){
        if (!$userId){
            $userId = self::getCurrentUser()->Id;
        }
    
        $model = new \haibao\cms\model\data\UserOperateHistory();
        $model->UserId = $userId;
        $model->OperateData = json_encode($operateData);
        $model->OperateType = $operateType;
        $model->OperateTime = new \DateTime();
        $data = new \haibao\cms\data\UserOperateHistory();
    
        $data->saveHistory($model);
    }

    public static function getCmsDataMap($appId, $dataType, $isAppId = true){

        $data = new \haibao\jiepai\data\Article();
        return $data->getCmsDataMap((int)$appId, (int)$dataType, $isAppId);
    }

    //处理敏感词
    public static function ReplaceSensitiveWords($content, $sensitiveWords)
    {
        //$content = '有颜色的机车外套很提亮肤色，版型修身干练，搭配美G的大长腿，真的是绝配！再戴上大酷墨\(sm\)镜，超模的标配就完美了！喜欢！[害羞][害羞]';
        //$sensitiveWords = '(\sm)###[sm]###uc***###';
        if ($sensitiveWords) {
            $sensitiveList = explode('###', $sensitiveWords);
            foreach ($sensitiveList as $key => $val) {
                //$sensitiveWords[$key] =  preg_replace_callback('/(\\|\[|\]|\*|\(|\))/isU', 'self::pregSensitiveWords', $val); 暂时未用
                $val = str_replace('\\', '', $val);
                $val = str_replace('[', '\\[', $val);
                $val = str_replace(']', '\\]', $val);
                $val = str_replace('*', '\\*', $val);
                $val = str_replace('(', '\\(', $val);
                $val = str_replace(')', '\\)', $val);
                $text = '';
                for ($i = 0; $i < mb_strlen($val,'utf-8'); $i++) {
                    if ($val[$i] != '\\') {
                        $text .= '*';
                    }
                }
                $content = str_replace($val, $text, $content);
            }
            return $content;
        } else {
            return $content;
        }
    }
}
